<script setup lang="ts">
import { ref, reactive, h } from "vue";
import { getListApi } from "/@/api/mes/craft";
import { useModal } from '/@/hooks/useModal'
import { useTableNotCheck } from '/@/hooks/useMessage'
import { dataFormatter } from '/@/utils/data'

// 表格配置
const tableRef = ref<YTableInstance>();
const tableProps = reactive<YTableProps>({
  height: "auto",
  columns: [
    { field: "code", label: "工单编号", filter: true },
    { field: "productId", label: "产品", formatter: (v) => dataFormatter(v, 'Product') },
    { field: "planStartDate", label: "计划开始", formatter: 'date' },
    { field: "quantity", label: "数量" },
    { field: "createTime", label: "创建时间", formatter: 'dateTime' },
    { field: "createUser", label: "创建人" },
    { field: "updateTime", label: "更新时间", formatter: 'dateTime' },
    { field: "updateUser", label: "更新人" },
    { field: "auditTime", label: "审核时间", formatter: 'dateTime' },
    { field: "isAudited", label: "审核", formatter: 'bool' },
  ],
  remoteConfig: {
    api: getListApi,
    immediate: true
  },
  operas: [
    {
      title: '修改', type: 'primary',
      onClick: () => {
        handEdit()
      }
    }
  ]
});

// 编辑窗口
const isChange = ref(false)
const viewModal = useModal({
  title: '工艺修改',
  width: 1000,
  height: 600,
  content: () => import('./view.vue'),
  contentOn: {
    onChange: () => {
      isChange.value = true
    }
  }
});

viewModal.on('open', () => {
  isChange.value = false
})
viewModal.on('close', () => {
  if (isChange.value) {
    tableRef.value.loadData()
  }
})

const handEdit = ({ row }: any = {}) => {

  let id: number;
  if (!row) {
    const selects = tableRef.value.vxe().getCheckboxRecords()
    if (!selects.length)
      return useTableNotCheck()

    id = selects[0].id
  } else {
    id = row.id
  }

  viewModal.open({
    contentProps: { id }
  })
}

</script>
<template>
  <IndexLayout>
    <YTable v-bind="tableProps" ref="tableRef" @row-dbl-click="handEdit"></YTable>
  </IndexLayout>
</template>